package org.suburban.servlet.staticdata;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.logging.Logger;
import org.suburban.data.StationRepository;
/**
 * Class to "parse" 'stations' static data  
 * @author Alexey Palamarchuk (slonnik@gmail.com)
 */
public class StationsCracker extends DefaultHandler{

	/** flag to indicate we start parsing the list of stations*/
	private boolean parsingList = false;
	/** flag to indicate we start parsing single station*/
	private boolean parsingItem = false;
	/** item to store data*/
	private final StationItem item = new StationItem();
	/** logger*/
	private static final Logger logger = Logger.getLogger(StationsCracker.class.getName());
	/** direction, to which stations belong*/
	private final String direction;
	
	/**
	 * ctor
	 * @param direction	- direction, to which stations belong 
	 */
	StationsCracker(final String direction)
	{
		this.direction = direction;
	}
	
	/** 
	 * class to store station description
	 */
	 class StationItem
	{
		/** station unique id*/
		String id;
		/** direction id, to which station belongs*/
		String directionId;
		/** index inside the direction*/
		int index = -1;
		/** human readable station name*/
		String name;
		/** station comments*/
		String comments;
		
		void clear()
		{
			id = null;
			directionId = direction;
			index = -1;
			name = null;
			comments = null;
		}
		@Override
		public String toString()
		{
			return String.format("id=%1$s; directionId=%2$s; index=%3$d; name=%4$s; comments=%5$s",
					id, directionId, index, name, comments);
		}
	}
	
	@Override
	public void startElement(String arg0, String arg1, String arg2,
			Attributes arg3) throws SAXException {
		
		/** arg2 means qualified name*/
		if(arg2.equalsIgnoreCase("select"))
		{
			String value = arg3.getValue("id");
			parsingList =(value != null) && (value.equalsIgnoreCase("id_station_from"));
			return;
		}
		if(!parsingList)
		{
			/**we still not parsing list of stations */
			return;
		}
		/** arg2 means qualified name*/
		if(arg2.equalsIgnoreCase("option"))
		{
			/** we start parsing station*/
			int index = item.index;
			item.clear(); //clear item first
			item.id = arg3.getValue("value");
			item.index = ++index;
			parsingItem = true;
		}
		
	}
	
	@Override
	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
		
		/** get item data*/
		if(parsingItem)
		{
			String value = new String(arg0, arg1, arg2);
			if(value.contains("("))
			{
				String[] data = value.split("\\(");
				item.name = data[0].trim();
				item.comments = "("+data[1];
			}
			else if(value.contains("["))
			{
				String[] data = value.split("\\[");
				item.name = data[0].trim();
				item.comments = "["+data[1];
			}
			else
			{
				item.name = value.trim();
				item.comments = "";
			}
		}
	}
	
	@Override
	public void endElement(String arg0, String arg1, String arg2)
			throws SAXException {
		
		/** arg2 means qualified name*/
		if(arg2.equalsIgnoreCase("select"))
		{
			parsingList = false;
			return;
		}
		/** arg2 means qualified name*/
		if(arg2.equalsIgnoreCase("option"))
		{
			/** trace station item*/
			logger.fine(item.toString());
			/** save station item in the database*/
			StationRepository.put(item.id, item.name, item.comments, item.directionId, item.index);
			
			parsingItem = false;
			return;
		}
		
	}
}
